# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： urllib_parse.py
    @date：2023/12/19 20:18
"""
# 请求模块
from urllib import request
# 编码模块
from urllib import parse

# 定义常用变量
url = 'https://www.baidu.com/s?'
headers = {'User-Agent': 'Mozilla/5.0'}

# 编码，拼接完整URL
word = input('请输入要搜索的内容:')
query_string = parse.urlencode({'wd': word,
                                'chrome': 'utf-8',
                                'f': 8,
                                'rsv_bp': 1,
                                'rsv_idx': 1,
                                'tn': 'baidu'})
url = url + query_string
# 创建请求对象

req = request.Request(url=url, headers=headers)
res = request.urlopen(req)
html = res.read().decode('utf-8')

# 文件保存
filename = '{}.html'.format(word)
with open(filename, 'w', encoding='utf-8') as f:
    f.write(html)
